package com.sas.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sas.entity.Page;
import com.sas.entity.PageBean;
import com.sas.entity.TCuohfkd;
import com.sas.service.TCuohfkdService;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * 
 * 
 * @author HuQiAn
 * @email 
 * @date 2018-05-26 22:27:19
 */
@Controller
@RequestMapping("cuohuofankui.do")
public class TCuohfkdController {
	@Autowired
	private TCuohfkdService tCuohfkdService;
	
	//分页查询
		@RequestMapping(params="method=ajax")
		public void listFeny(String tradedate,String wangwid,String pageNo,String pageSize ,HttpSession session,HttpServletResponse response) throws IOException {
		    Page p = new Page();
			//每页条数
			p.setSize(Integer.parseInt(pageSize));
			//调用service总条数
			Integer sum = tCuohfkdService.selectCount();
			//总页数
			Integer pages = sum%p.getSize()!=0?sum/p.getSize()+1:sum/p.getSize();
			
			p.setCount(pages);
			int index_int = 1;
			if(pageNo==null||pageNo==""){
				index_int = 1;
			}else{
				index_int = Integer.parseInt(pageNo);
				if(index_int<=0){
					index_int = 1;
				}
				if(index_int > p.getCount()){
					index_int = p.getCount();
				}
			}
			
			p.setIndex(index_int);
			TCuohfkd s=new TCuohfkd();
			s.setTradedate(tradedate);
			s.setWangwid(wangwid);
			List<TCuohfkd> list =tCuohfkdService.listFenYe(s,p);
			 ObjectMapper mapper = new ObjectMapper();    //提供java-json相互转换功能的类
			 
		      //将list中的对象转换为Json格式的数组	     
		  PageBean<TCuohfkd> pg=new PageBean<TCuohfkd>();  
		  pg.setTotal(sum);
		  pg.setRows(list);
		  String json = mapper.writeValueAsString(pg);
		        //将json数据返回给客户端
		      response.setContentType("text/html; charset=utf-8");
		       response.getWriter().write(json);  
			//存入session数据
		   //  session.setAttribute("list", list);
		  //   session.setAttribute("page", p);
		    //返回页面
		//	return "jsp/cuohuofkd";
		}
	//集合查询
    @RequestMapping(params="xxxx路径1")
	public String listselect(HttpSession session){
	//tCuohfkd list=tCuohfkdService.listselect();
	//存入session数据
	//session.setAttribute("list", list);
	//返回页面
		return "yemian";
	}
    //增加
    @RequestMapping(params="method=insert")
	public void insert(String tradedate,String dindanid,String wangwid,String wenti,String chulifa,String chulijg,HttpSession session,HttpServletResponse response) throws IOException {
	TCuohfkd aa=new TCuohfkd( tradedate, dindanid, wangwid, wenti, chulifa, chulijg);
		int h=tCuohfkdService.insert(aa);
		ObjectMapper mapper = new ObjectMapper();    //提供java-json相互转换功能的类  
	     String json = mapper.writeValueAsString(h);
	        //将json数据返回给客户端
	       response.setContentType("text/html; charset=utf-8");
	       response.getWriter().write(json);
	}
    //删除
	@RequestMapping(params="method=delete")
	public void delete(String id,HttpSession session,HttpServletResponse response) throws IOException {
		
		TCuohfkd aa=new TCuohfkd();
		int parseInt = Integer.parseInt(id);		
		aa.setId(parseInt);
		int delete = tCuohfkdService.delete(aa);
		 ObjectMapper mapper = new ObjectMapper();    //提供java-json相互转换功能的类  
	     String json = mapper.writeValueAsString(delete);
	        //将json数据返回给客户端
	      response.setContentType("text/html; charset=utf-8");
	       response.getWriter().write(json);
	}
    //修改
	@RequestMapping(params="method=update")
	public void update(int id, String tradedate,String dindanid,String wangwid,String wenti,String chulifa,String chulijg,HttpServletResponse response) throws IOException  {
		TCuohfkd aa=new TCuohfkd(id, tradedate, dindanid, wangwid, wenti, chulifa, chulijg);
		System.out.println("参数"+aa.toString());
		int h=tCuohfkdService.update(aa);
		 ObjectMapper mapper = new ObjectMapper();    //提供java-json相互转换功能的类  
	     String json = mapper.writeValueAsString(h);
	        //将json数据返回给客户端
	      response.setContentType("text/html; charset=utf-8");
	       response.getWriter().write(json);
	}
    //分页查询
	@RequestMapping(params="method=selectByFenye")
	public String listFenYe(String index,String xx,HttpSession session) {
	    Page p = new Page();
		//每页条数
		p.setSize(20);
		//调用service总条数
		Integer sum = tCuohfkdService.selectCount();
		//总页数
		Integer pages = sum%p.getSize()!=0?sum/p.getSize()+1:sum/p.getSize();
		
		p.setCount(pages);
		int index_int = 1;
		if(index==null||index==""){
			index_int = 1;
		}else{
			index_int = Integer.parseInt(index);
			if(index_int<=0){
				index_int = 1;
			}
			if(index_int > p.getCount()){
				index_int = p.getCount();
			}
		}
		
		p.setIndex(index_int);
		TCuohfkd s=new TCuohfkd();
		List<TCuohfkd> list =tCuohfkdService.listFenYe(s,p);
		//存入session数据
	    session.setAttribute("list", list);
	    session.setAttribute("page", p);
	    //返回页面
		return "jsp/cuohuofkd";
	}
    //统计条数
	@RequestMapping(params="xxxx路径6")
	public void selectCount() {
		int h=tCuohfkdService.selectCount();
	}
	/*@RequestMapping(params="method=dayinchukudan")
	public void dayinchukudan(HttpServletResponse response) throws Exception{
		List<Chukudan> list = ShangpinService.selectChukudan();
		//
		response.setContentType("application/vnd.ms-excel");
		Date date = new Date();//设置日期
		DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//ʱ���ʽ��
		String d = dFormat.format(date);//截取日期
		String title = d+"出库单";
		//�ļ���
		String fileName = URLEncoder.encode("出库单","utf-8");
		response.setHeader("Content-disposition", "attachment;fileName="+fileName+".xls");
		//输出流
		OutputStream out = response.getOutputStream();
		
		WritableWorkbook wb = Workbook.createWorkbook(out);
		WritableSheet sheet = wb.createSheet("出库单", 0);
		sheet.getSettings().setDefaultColumnWidth(30);//50*256
		sheet.getSettings().setDefaultRowHeight(10);
		
		WritableFont font = new WritableFont(WritableFont.ARIAL,16,WritableFont.NO_BOLD);
		
		WritableCellFormat format = new WritableCellFormat(font);
		
		format.setBorder(Border.ALL, BorderLineStyle.THIN);
		
		format.setAlignment(Alignment.CENTRE);
		
		format.setWrap(true);
		
		WritableFont font1 = new WritableFont(WritableFont.ARIAL,30,WritableFont.BOLD);
		WritableCellFormat format1 = new WritableCellFormat(font1);
		format1.setAlignment(Alignment.CENTRE);
		
		sheet.mergeCells(0, 0, 2, 0);
		Label t  = new Label(0,0,title,format1);
		sheet.addCell(t);
		
	
		Label t1 = new Label(0,1,"供应商名字",format);
		Label t2 = new Label(1,1,"商品名字",format);		
		Label t3 = new Label(2,1,"地址",format);
		Label t4 = new Label(3,1,"备注",format);
		sheet.addCell(t1);
		sheet.addCell(t2);
		sheet.addCell(t3);
		sheet.addCell(t4);				
		int i = 0;		
		for (Chukudan Shangpin : list) {			
			Label c1 = new Label(0,i+2,Shangpin.getGysname()+"",format);
			Label c2 = new Label(1,i+2,Shangpin.getSpname()+"",format);
			Label c3 = new Label(2,i+2,Shangpin.getUrl()+"",format);
			Label c4 = new Label(3,i+2,Shangpin.getSpsl()+"",format);	
			sheet.addCell(c1);
			sheet.addCell(c2);
			sheet.addCell(c3);
			sheet.addCell(c4);
			i++;
		}	
		wb.write();
		wb.close();
		out.flush();
		out.close();
	} */
}
